﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CongregacaoOs.Data;
using CongregacaoOs.Business;

namespace CongregacaoOs.WebForm.Cadastros
{
    public partial class Distrital : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            

            if (Session["DadosGerais"] == null) Response.Redirect("~/Cadastros/Account/Login.aspx");

            if (Session["TipoUsuario"].ToString() == "Local") Response.Redirect("~/Cadastros/Home.aspx");

            Page.Form.DefaultButton = btnBuscar.UniqueID;

            if (!IsPostBack)
            {
                AtualizarGridView();

                AtualizarDropRegionalFiltro();
                AtualizarDropDowListPais();
            }
        }

        public void AtualizarGridView()
        {
            ccDistritalBus bus = new ccDistritalBus();
            ccDistrital ent = new ccDistrital();

            ent.idRegional = dropRegionalFiltro.SelectedValue != "" ? Convert.ToInt32(dropRegionalFiltro.SelectedValue) : 0;
            ent.nome = txtBuscarNome.Text;
            gvDistrital.DataSource = bus.DistritalSelect(ent);
            gvDistrital.DataBind();
        }


        public void AtualizarDropDowListPais(int aID = 0)
        {
            ccPaisBus bus = new ccPaisBus();
            ccPais ent = new ccPais();
            ent.idPais = aID;
            dropPais.DataSource = bus.PaisSelect(ent);
            dropPais.DataBind();
        }

        public void AtualizarDropDowListEstado(int aID = 0)
        {
            ccEstadoBus busEstado = new ccEstadoBus();
            ccEstado entEstado = new ccEstado();
            entEstado.idEstado = aID;

            if (dropPais.SelectedValue != "")
            {
                entEstado.idPais = Convert.ToInt32(dropPais.SelectedValue);
                entEstado.nome = "";
                entEstado.sigla = "";
                dropEstado.DataSource = busEstado.EstadoSelect(entEstado);
                dropEstado.DataBind();
            }
            else
            {
                dropEstado.Items.Clear();
            }
        }

        public void AtualizarDropDowListRegional(int aID = 0)
        {
            ccRegionalBus bus = new ccRegionalBus();
            ccRegional ent = new ccRegional();
            ent.idRegional = aID;

            if (dropEstado.SelectedValue != "")
            {
                ent.idPais = dropPais.SelectedValue != "" ? Convert.ToInt32(dropPais.SelectedValue) : 0;
                ent.idEstado = Convert.ToInt32(dropEstado.SelectedValue);  
                
                dropRegional.DataSource = bus.RegionalSelect(ent);
                dropRegional.DataBind();
            }
            else
            {
                dropRegional.Items.Clear();
            }

        }

        public void AtualizarDropRegionalFiltro()
        {
            using (ccRegionalBus bus = new ccRegionalBus())
            {

                ccRegional ent = new ccRegional();

                ent.nome = "";
                ent.idPais = 0;
                ent.idEstado = 0;
                ent.idRegional = 0;
                dropRegionalFiltro.DataSource = bus.RegionalSelect(ent);
                dropRegionalFiltro.DataBind();

            }
        }


        public void LimparCampos(Control controle)
        {
            foreach (Control ctrl in controle.Controls)
            {
                if (ctrl is TextBox) ((TextBox)ctrl).Text = string.Empty;
                else if (ctrl.Controls.Count > 0)
                {
                    LimparCampos(ctrl);
                }
            }
        }


        protected void gvDistrital_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            AtualizarGridView();
            gvDistrital.PageIndex = e.NewPageIndex;
            gvDistrital.DataBind();
        }

        protected void gvDistrital_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EDITAR" || e.CommandName == "EXCLUIR")
            {
                GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);

                int index = row.RowIndex;

                hdID.Value = gvDistrital.Rows[index].Cells[1].Text.Trim();
                HiddenField hdIdPais = (gvDistrital.Rows[index].FindControl("hdIdPais") as HiddenField);
                HiddenField hdIdEstado = (gvDistrital.Rows[index].FindControl("hdIdEstado") as HiddenField);
                HiddenField hdIdRegional = (gvDistrital.Rows[index].FindControl("hdIdRegional") as HiddenField);

                if (e.CommandName == "EDITAR")
                {
                    hfIndexAccordion.Value = "1";
                    txtNome.Text = gvDistrital.Rows[index].Cells[2].Text.Trim();

                    AtualizarDropDowListPais();
                    dropPais.SelectedValue = hdIdPais.Value;
                    AtualizarDropDowListEstado();
                    dropEstado.SelectedValue = hdIdEstado.Value;
                    AtualizarDropDowListRegional();
                    dropRegional.SelectedValue = hdIdRegional.Value;

                }
                else if (e.CommandName == "EXCLUIR")
                {
                    ccDistritalBus bus = new ccDistritalBus();
                    bus.DistritalDelete(Convert.ToInt32(hdID.Value));
                }
            }

            AtualizarGridView();
        }

        protected void dropPais_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropDowListEstado();
            AtualizarDropDowListRegional();
        }

        protected void dropEstado_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropDowListRegional();
        }

        protected void gvDistrital_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgEditar = (e.Row.FindControl("imgEditar") as ImageButton);
                imgEditar.CommandArgument = e.Row.RowIndex.ToString();

                ImageButton imgDelete = (e.Row.FindControl("imgDelete") as ImageButton);
                imgDelete.CommandArgument = e.Row.RowIndex.ToString();

                imgDelete.Attributes.Add("onclick", "javascript:return " +
                        "confirm('Confirmar exclusão do registro:" +
                        DataBinder.Eval(e.Row.DataItem, "distrital") + "?')");
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {

            try
            {
                ccDistritalBus bus = new ccDistritalBus();
                ccDistrital ent = new ccDistrital();

                ent.nome = txtNome.Text.Trim();
                ent.idPais = Convert.ToInt32(dropPais.SelectedValue);
                ent.idEstado = Convert.ToInt32(dropEstado.SelectedValue);
                ent.idRegional = Convert.ToInt32(dropRegional.SelectedValue);

                if (hdID.Value != "")
                    ent.idDistrital = Convert.ToInt32(hdID.Value);

                bus.DistritalSaveOrUpdate(ent);
                hfIndexAccordion.Value = "0";
                AtualizarGridView();
                Functions.LimparCampos(this);
                dropEstado.Items.Clear();
                dropRegional.Items.Clear();

                MessageWindow.MessageType = true;
                MessageWindow.Texto = "Registro salvo com sucesso!!!";
                MessageWindow.ShowWindow();
            }
            catch (Exception)
            {

                MessageWindow.MessageType = false;
                MessageWindow.Texto = "Houve um erro ao salvar o registro. Tente novamente ou contate o suporte!!!";
                MessageWindow.ShowWindow();
            }

        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            hfIndexAccordion.Value = "0";
            hdID.Value = "";
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            AtualizarGridView();
        }

        protected void btnAdicionarNovo_Click(object sender, EventArgs e)
        {
            hfIndexAccordion.Value = "1";
            AtualizarDropDowListPais();
            AtualizarDropDowListEstado();
            AtualizarDropDowListRegional();
            hdID.Value = "";
            LimparCampos(this);
        }

        protected void dropPais_DataBound(object sender, EventArgs e)
        {
            dropPais.Items.Insert(0, new ListItem("Escolha a País...", String.Empty));
        }

        protected void dropEstado_DataBound(object sender, EventArgs e)
        {
            dropEstado.Items.Insert(0, new ListItem("Escolha a Estado...", String.Empty));
        }

        protected void dropRegional_DataBound(object sender, EventArgs e)
        {
            dropRegional.Items.Insert(0, new ListItem("Escolha a Regional...", String.Empty));
        }

        protected void dropRegionalFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarGridView();
        }

        protected void dropRegionalFiltro_DataBound(object sender, EventArgs e)
        {
            dropRegionalFiltro.Items.Insert(0, new ListItem("Escolha a Regional...", String.Empty));
        }

    }
}